package cn.edu.cqu.fredyvia.forum.controller;

import cn.edu.cqu.fredyvia.forum.config.AliyunOssConfig;
import cn.edu.cqu.fredyvia.forum.dto.TalkUserinfoDTO;
import cn.edu.cqu.fredyvia.forum.entity.Talk;
import cn.edu.cqu.fredyvia.forum.entity.enums.NiceEnum;
import cn.edu.cqu.fredyvia.forum.entity.enums.TalkTypeEnum;
import cn.edu.cqu.fredyvia.forum.service.ICommentService;
import cn.edu.cqu.fredyvia.forum.service.ITalkService;
import cn.edu.cqu.fredyvia.forum.service.IUserService;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/talk")
public class TalkController extends BaseController {

    @Autowired
    AliyunOssConfig aliyunOssConfig;

    @Autowired
    IUserService iUserService;

    @Autowired
    ITalkService talkService;

    @Autowired
    ICommentService iCommentService;

    @GetMapping("/info")
    @ResponseBody
    public AjaxResult info(@RequestParam Integer talkId) {
        logger.info(talkId.toString());
        TalkUserinfoDTO talkUserinfoDTO = talkService.getUserinfoDto(talkId);
        return AjaxResult.success(talkUserinfoDTO);
    }

    @GetMapping("/count")
    @ResponseBody
    public AjaxResult count(){
        return AjaxResult.success(talkService.count());
    }


    //默认时间排序
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(@RequestParam(defaultValue = "0") Integer from, @RequestParam(defaultValue = "5") Integer to
            , @RequestParam(defaultValue = "create_datetime") String sortby) {
        try {
            if (!sortby.equalsIgnoreCase("create_datetime")){
                sortby = "total_comment";
            }
            List<TalkUserinfoDTO> list = talkService.select_inject(from, to, sortby);
//            for (TalkUserinfoDTO talkUserinfoDTO : list) {
//                forum-storage.oss-cn-chengdu.aliyuncs.com/1626612751096-acb42fab144d448bbd7845e9b908eaa3.jpg
//                logger.info(talkUserinfoDTO.getAvatar());
//                String newAvatar = "https://" + aliyunOssConfig.getBucketname() + "." + aliyunOssConfig.getEndpoint() + "/" + talkUserinfoDTO.getAvatar();
//                talkUserinfoDTO.setAvatar(newAvatar);
//                logger.info(talkUserinfoDTO.getAvatar());
//            }
            logger.info("talk_list:"+list.toString());
            return AjaxResult.success(list);
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.error("加载完了");
        }
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map<String, Object> map) {
//        Talk talk = BeanUtils.toBean(map,Talk.class);
//        System.out.println(map);
        try {
            String s = JSON.toJSONString(map);
            logger.info(s);
            Talk talk = JSON.parseObject(s, Talk.class);
            logger.info(talk.toString());
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();
            talk.setUserId(iUserService.getUserByUsername(username).getId());
            if(talk.getType()==null)
                talk.setType(TalkTypeEnum.OTHER.getIndex());
            talk.setNice(NiceEnum.DEFAULT);
            talk.setId(null);
            //保存content至obs
            String contentPath = "https://" + aliyunOssConfig.getBucketname() + "." +
                    aliyunOssConfig.getEndpoint() + "/"+talkService.save2oss(talk.getContent());
            talk.setContent(contentPath);
            //System.out.println(talk);
            return toAjax(talkService.save(talk));
        } catch (Exception e) {
            return AjaxResult.error("参数不全");
        }
    }
}
